package com.pactera.asmp.server.service;

import com.pactera.asmp.server.common.utils.FastJsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @ProjectName: tlsserver
 * @Package com.pactera.asmp.server.service.redis.impl
 * @ClassName: RedisServiceImpl
 * @Description: RedisService实现
 * @Date 2020/05/30 11:12
 * @Author lvzhiqiang
 * @Version 1.0
 */
@Service
public class RedisServiceImpl implements IRedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    private Random r = new Random();

    @Override
    public void set(String key, String value) {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, value);
    }

    @Override
    public boolean set(String key, Object value, int db, boolean flagJson, Long timeOut) {
        boolean result = false;
        try {
            RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
            DefaultStringRedisConnection stringRedisConnection = new DefaultStringRedisConnection(redisConnection);
            stringRedisConnection.select(db);
            if(flagJson){
                stringRedisConnection.set(key, FastJsonUtil.BeanToJson(value));
            }else{
                stringRedisConnection.set(key, value.toString());
            }
            if(timeOut != null && timeOut != 0){
                stringRedisConnection.expire(key, timeOut);
            }
            stringRedisConnection.close();
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public Object get(String key, int db) {
        Object result = null;
        try {
            RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
            DefaultStringRedisConnection stringRedisConnection = new DefaultStringRedisConnection(redisConnection);
            stringRedisConnection.select(db);
            result = stringRedisConnection.get(key);
            stringRedisConnection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public void setex(String key, String value, TimeUnit timeUnit, int time) {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, value,time,timeUnit);
//        redisTemplate.expire(key, time, timeUnit);
    }

    @Override
    public void hset(String hashKey, String subKey, String value, long time, int dbIndex) {
        try {
            RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
            DefaultStringRedisConnection stringRedisConnection = new DefaultStringRedisConnection(redisConnection);
            stringRedisConnection.select(dbIndex);
            stringRedisConnection.hSet(hashKey, subKey, value);
            stringRedisConnection.expire(hashKey, time);
            stringRedisConnection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    @Override
    public void remove(String key, int db) {
        try {
            RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
            DefaultStringRedisConnection stringRedisConnection = new DefaultStringRedisConnection(redisConnection);
            stringRedisConnection.select(db);
            if(stringRedisConnection.exists(key)){
                stringRedisConnection.del(key);
            }
            stringRedisConnection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void remove(String key) {
        redisTemplate.delete(key);
    }

    @Override
    public StringRedisTemplate getRedisTemplate() {
        return redisTemplate;
    }

    @Override
    public int getExpire(int exprie, int randomScope) {
        if(randomScope > 0) {
            exprie = exprie + r.nextInt(randomScope);
        }
        return exprie;
    }

}
